import jmespath

from config_params.base_util import baseUtil
import html

class PreProcessor:

    def __init__(self, data):
        self.data = data

    def jme_path(self, exp):
        return jmespath.search(exp, self.data)

    def JSR223PreProcessor(self, class_name):
        jsr233_dict = {}
        jsr233_dict["name"] = self.jme_path("name") if self.jme_path("name") is not None else "JSR223 PreProcessor"
        jsr233_dict["cache_key"] = self.jme_path("cache_key") if self.jme_path("cache_key") is not None else True
        jsr233_dict["filename"] = self.jme_path("filename") if self.jme_path("filename") is not None else ""
        jsr233_dict["parameters"] = self.jme_path("parameters") if self.jme_path("parameters") is not None else ""
        jsr233_dict["script"] = html.escape(self.jme_path("script") if self.jme_path("script") is not None else "")
        jsr233_dict["comments"] = self.jme_path("comments") if self.jme_path("comments") is not None else ""
        jsr233_dict["is_enabled"] = self.jme_path("is_enabled") if self.jme_path("is_enabled") is not None else True

        ScriptLanguage = baseUtil.get_instance_attr("ScriptLanguage")
        if self.jme_path("script_language") == "groovy":
            jsr233_dict["script_language"] = ScriptLanguage.GROOVY
        elif self.jme_path("script_language") == "beanshell":
            jsr233_dict["script_language"] = ScriptLanguage.BEANSHELL
        elif self.jme_path("script_language") == "bas":
            jsr233_dict["script_language"] = ScriptLanguage.BSH
        elif self.jme_path("script_language") == "ecmasript":
            jsr233_dict["script_language"] = ScriptLanguage.ECMASCRIPT
        elif self.jme_path("script_language") == "java":
            jsr233_dict["script_language"] = ScriptLanguage.JAVA
        elif self.jme_path("script_language") == "javascript":
            jsr233_dict["script_language"] = ScriptLanguage.JS
        elif self.jme_path("script_language") == "jexl":
            jsr233_dict["script_language"] = ScriptLanguage.JEXL
        elif self.jme_path("script_language") == "jexl2":
            jsr233_dict["script_language"] = ScriptLanguage.JEXL2
        constant_timer_obj = baseUtil.create_instance(class_name, jsr233_dict)
        return constant_timer_obj

    def preProcessor(self):
        preProcessors_list = []
        if self.jme_path("type") == "JSR223PreProcessor":
            preProcessors_list.append(self.JSR223PreProcessor(self.jme_path("type")))
        return preProcessors_list
